package com.qf.controller;

import com.qf.common.http.Result;
import com.qf.entiy.dto.Goods;
import com.qf.entiy.po.LoginMerchant;
import com.qf.service.impl.GoodsService;
import com.qf.service.impl.GoodsSkuService;
import com.qf.service.impl.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsTypeService goodsTypeService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsSkuService goodsSkuService;



    @RequestMapping("list")
    public String toList(Model model, LoginMerchant loginMerchant) {
        model.addAttribute("goodsList", goodsService.getByMerchant(loginMerchant.getId()));
        return "goods/list";
    }

    @RequestMapping("add")
    public String toAdd(Model model,@RequestParam(value = "id",required = false) Long id) {
        model.addAttribute("goodsTypeList", goodsTypeService.list());

        if (id != null){
            model.addAttribute("goods",goodsService.getById(id));
            model.addAttribute("skuList",goodsSkuService.getSkuListByGoodsId(id));
        }else{
            Goods goods = new Goods();
            goods.setOrderNum((long) goodsService.count()+1);
            model.addAttribute("goods",goods);
        }
        return "goods/add";
    }

    @PostMapping("del")
    @ResponseBody
    public Result<?> del(@RequestParam(value = "id", required = true) Long id) {
        goodsService.removeGoods(id);
        return Result.success();
    }

    //yum文件设置上传的文件路径
    @Value("${upload.path}")
    private String path;

    //文件上传
    @PostMapping("upload")
    @ResponseBody
    public Result<?> upload(MultipartFile file) {
        int start = file.getOriginalFilename().lastIndexOf(".");
        String suffix = file.getOriginalFilename().substring(start);
        String fileName = String.format("%s%s", UUID.randomUUID().toString(), suffix);
        try {
            file.transferTo(new File(String.format("%s%s", path, fileName)));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //返回地址,需要开启nginx(反向代理)
        return Result.success(String.format("http://192.168.51.5:8080/%s", fileName));
    }
    //保存商品和sku属性
    @PostMapping("save")
    @ResponseBody
    public Result<?> save(Goods goods, LoginMerchant loginMerchant) {
        goodsService.saveGoods(goods, loginMerchant);

        return Result.success();
    }

}
